package util;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import mode.User;

public class SocketUtil {
	public static Socket socket;
	public static User user=new User();
	public static Reciver reciver;
	
	public static void startServer() {
		ServerSocket server;
		try {
			server=new ServerSocket(9988);
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						socket=server.accept();
						reciver.startThread();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
		} catch (Exception e) {
			;
		}
	}

	public static void sendMessage(Object message){
		ObjectOutputStream oos;
		try {
			oos=new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(message);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setSocket(Socket socket) {
		SocketUtil.socket = socket;
		System.out.println("Client connected!");
		reciver.startThread();
	}
	
	public static Reciver getReciver() {
		return reciver;
	}

	public static void setReciver(Reciver reciver) {
		SocketUtil.reciver = reciver;
	}

	public static Socket getSocket() {
		return socket;
	}
	
	public static User getUser() {
		return user;
	}

	public static void setUser(User user) {
		SocketUtil.user = user;
	}
}
